package com.sx.micowell.framework.adapter.binding;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.databinding.BindingAdapter;

public class WebViewBinding {
    @BindingAdapter(value = {"webViewData"})
    public static void setWebData(WebView webView, String data) {
        if (webView != null) {
            WebSettings settings = webView.getSettings();
            settings.setJavaScriptEnabled(true);
            ////提高渲染的优先级
            settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
            //将图片调整到适合webview的大小
            settings.setUseWideViewPort(true);
            settings.setLoadWithOverviewMode(true);
            //不支持缩放
            settings.setSupportZoom(false);
            //隐藏原生缩放
            settings.setDisplayZoomControls(true);
            ////支持自动加载图片
            settings.setTextZoom(200);
            ;
            settings.setLoadsImagesAutomatically(true);
            //设置缓存 只能网络加载
            settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
            //设置操作文件
            settings.setAllowFileAccess(true);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setSupportMultipleWindows(true);
            settings.setAppCacheEnabled(true);
            settings.setDomStorageEnabled(true);
            settings.setBlockNetworkImage(false);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    boolean isFinish=true;
                    if (url.startsWith("weixin://wap/pay")) {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        webView.getContext().startActivity(intent);
                        return true;
                    }
                    if (url.startsWith("alipays:") || url.startsWith("alipay")) {
                        try {
                            Uri parse = Uri.parse(url);
                            webView.getContext().startActivity(new Intent("android.intent.action.VIEW",parse ));
                        } catch (Exception e) {
                            isFinish=false;
                            new AlertDialog.Builder( webView.getContext())
                                    .setMessage("未检测到支付宝客户端，请安装后重试。")
                                    .setPositiveButton("立即安装", new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            Uri alipayUrl = Uri.parse("https://d.alipay.com");
                                            webView.getContext().startActivity(new Intent("android.intent.action.VIEW", alipayUrl));
                                        }
                                    }).setNegativeButton("取消", null).show();
                        }

                        finally {
                            if(isFinish&&webView.getContext() instanceof Activity){
                                Activity activity= (Activity) webView.getContext();
                                if(!activity.isDestroyed()){
                                    activity.finish();
                                }

                            }
                        }
                        return true;
                    }
                    if (!(url.startsWith("http") || url.startsWith("https"))) {
                        return true;
                    }
                    view.loadUrl(url);
                    return true;
                }
            });
            if (!TextUtils.isEmpty(data)) {
                webView.loadDataWithBaseURL(null, data, "text/html", "UTF-8", null);
            }
        }
    }
}
